package com.abaaba.project2.service.wx.wxserviceimpl;

import com.abaaba.project2.bean.*;
import com.abaaba.project2.bean.vo.wx.WxHelperVO;
import com.abaaba.project2.mapper.GoodsMapper;
import com.abaaba.project2.mapper.KeywordMapper;
import com.abaaba.project2.mapper.SearchHistoryMapper;
import com.abaaba.project2.service.wx.WxSearchModuleService;
import com.abaaba.project2.util.UserIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.lang.System;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Hedgehog
 * @Date 2021/6/8 16:15
 * @Version 1.0
 */
@Service
public class WxSearchModuleServiceImpl implements WxSearchModuleService {

    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    //这个不应该这么实现，暂时放在这
    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    KeywordMapper keywordMapper;

    @Override
    public void setHistory(String keyword) {


        SearchHistory searchHistory = new SearchHistory();
        Date date = new Date(System.currentTimeMillis());
        //TODO
        //这里需要把user_id拼接进去,从session域中拿出来
        searchHistory.setUserId(1);
        searchHistory.setKeyword(keyword);
        searchHistory.setDeleted(false);
        searchHistory.setAddTime(date);
        //TODO
        //不知道这里写什么form
        searchHistory.setFrom("wx");
        searchHistory.setUpdateTime(date);
        //怎么实现两个时间不一样
        //TODO
        searchHistoryMapper.insert(searchHistory);

    }

    @Override
    public void clearHistory() {

        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        //TODO
        //从session中拿到userid
        /*SearchHistory searchHistory = new SearchHistory();
        searchHistory.setDeleted(true);
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        criteria.andUserIdEqualTo(1);
        searchHistoryMapper.updateByExampleSelective(searchHistory,searchHistoryExample);
        */
        Integer userId = UserIdUtil.getUserId();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        searchHistoryMapper.deleteByExample(searchHistoryExample);

    }

    @Override
    public WxHelperVO helper(String keyword) {

        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andKeywordLike(keyword+"%");

        ArrayList<String> strings = new ArrayList<>();
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        for (Keyword keyword1 : keywords) {
            strings.add(keyword1.getKeyword());
        }
        WxHelperVO wxHelperVO = new WxHelperVO(strings);


        /*GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andNameLike(keyword+"%");*/


       /* ArrayList<String> strings = new ArrayList<>();
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        //不知道这里的vo返回值是什么
        for (Goods good : goods) {
            strings.add(good.getName());
        }

        WxHelperVO wxHelperVO = new WxHelperVO(strings);*/

        return wxHelperVO;
    }
}
